<?php

class UserController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$users = User::paginate(10);
		return View::make('users.index')->with(array('users' => $users));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$user = new User;
		return View::make('users.edit')->with(array('user' => $user, 'method' => 'post'));
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		// Get all the inputs
        // id is used for login, username is used for validation to return correct error-strings
        $userdata = array(
            'username' => Input::get('username'),
            'password' => Input::get('password'),
			'confirm_password' => Input::get('confirm_password'),
			'first_name' => Input::get('first_name'),
			'last_name' => Input::get('last_name'),
			'email' => Input::get('email'),
			'role_id' => Input::get('role_id'),
			'department_id' => Input::get('department_id')
        );

        // Declare the rules for the form validation.
        $rules = array(
            'username'  => 'Required|unique:users',
            'password'  => 'Required|min:8',
            'confirm_password'  => 'Required|same:password',
            'first_name'  => 'Required',
            'email'  => 'required|email|unique:users',
            'role_id'  => 'Required|exists:roles,id',
            'department_id'  => 'Required|exists:departments,id'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);

        // Check if the form validates with success.
        if ($validator->passes()) {
			if (User::create(Input::all()))
			{
                // Redirect to homepage
                return Redirect::to('users')->with('success', 'New user successfully added');
            }
			else
			{
				// Redirect to the login page.
                return Redirect::to('users.edit')->withErrors(array('message' => 'Opps something wrong!!'));
			}
		}
		
        // Something went wrong.
        return Redirect::to('users.edit')->with(array('user' => $user, 'method' => 'post'))
										 ->withErrors($validator);
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$user = User::findOrFail($id);
		return View::make('users.show')->with('user', $user);
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$user = User::findOrFail($id);
		return View::make('users.edit')->with(array('user' => $user, 'method' => 'put'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		// Retrieve current user
		$user = User::find($id);
	
		// Get all the inputs
        $userdata = array(
			'first_name' => Input::get('first_name'),
			'last_name' => Input::get('last_name'),
			'email' => Input::get('email'),
			'role_id' => Input::get('role_id'),
			'department_id' => Input::get('department_id')
        );

        // Declare the rules for the form validation.
        $rules = array(
            'first_name'  => 'Required',
            'email'  => 'required|email',
            'role_id'  => 'Required|exists:roles,id',
            'department_id'  => 'Required|exists:departments,id'
        );

        // Validate the inputs.
        $validator = Validator::make($userdata, $rules);

        // Check if the form validates with success.
        if ($validator->passes()) {
					
			if ($user->update(Input::all()))
			{
                // Redirect to homepage
                return Redirect::to('users')->with('success', 'User successfully Updated');
            }
			else
			{
				// Redirect to the login page.
                return Redirect::to('users.edit')->withErrors(array('message' => 'Opps something wrong!!'));
			}
		}
		
        // Something went wrong.
        return Redirect::to('users/'.$user->id.'/edit')->with(array('user' => $user, 'method' => 'put'))
										 ->withErrors($validator);
	}

	/**
	 * Show the form for delete the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function delete($id)
	{
		$user = User::findOrFail($id);
		return View::make('users.edit')->with(array('user' => $user, 'method' => 'delete'));
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Retrieve current user
		$user = User::find($id);
		$user->delete();
		
		return Redirect::to('users')->with('message', 'Successfully deleted user!');
	}


}
